import {createRouter,createWebHashHistory} from 'vue-router';
import Home from '../components/Home.vue';
import Login from '../components/Login.vue';


let router = createRouter({
    history:createWebHashHistory(),
    routes:[
        {
            path:"/",
            redirect:"/home"
        },
        {
            path:"/home",
            component:Home
        },
        {
            path:"/login",
            component:Login
        }
    ]
})


//路由守卫（前置）
router.beforeEach((to,from,next)=>{
   //to 去哪 .path地址
   //from 从哪 .path地址
   //next next() 放行  next(/地址) 拦截并转发
   if(to.path == '/login'){
      //放行
      next();
   }else{
      let account = window.localStorage.getItem("user");
      if(account){
        //不为null
         next();
      }else{
         next("/login");
      }
   }
})


export default router;